package vote.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.stereotype.Service;
import vote.domain.entity.User;
import vote.mapper.UserMapper;
import vote.service.UserService;

/**
 * Description:
 * Author:han
 * Date: 2024-05-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    //微信服务接口地址
    public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";


    @Override
    public void auth(WxOAuth2UserInfo userInfo) {
        //查询用户
        User user = this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, userInfo.getOpenid()));
        if (null == user) {
            user = new User();
            user.setOpenId(userInfo.getOpenid());
            user.setAvatar(userInfo.getHeadImgUrl());
            user.setName(userInfo.getNickname());
            this.save(user);
        }
        StpUtil.login(user.getOpenId());
        String token = StpUtil.getTokenValue();
    }
}
